/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dao.exceptions.IllegalOrphanException;
import dao.exceptions.NonexistentEntityException;
import dao.exceptions.RollbackFailureException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
import pojo.Category;
import pojo.Extra;
import pojo.Extraprice;
import pojo.Orderdetail;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author A7med Kotb
 */
public class ExtrapriceJpaController implements Serializable {

    public ExtrapriceJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Extraprice extraprice) throws RollbackFailureException, Exception {
        if (extraprice.getOrderdetailCollection() == null) {
            extraprice.setOrderdetailCollection(new ArrayList<Orderdetail>());
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Category categoryId = extraprice.getCategoryId();
            if (categoryId != null) {
                categoryId = em.getReference(categoryId.getClass(), categoryId.getId());
                extraprice.setCategoryId(categoryId);
            }
            Extra extraId = extraprice.getExtraId();
            if (extraId != null) {
                extraId = em.getReference(extraId.getClass(), extraId.getId());
                extraprice.setExtraId(extraId);
            }
            Collection<Orderdetail> attachedOrderdetailCollection = new ArrayList<Orderdetail>();
            for (Orderdetail orderdetailCollectionOrderdetailToAttach : extraprice.getOrderdetailCollection()) {
                orderdetailCollectionOrderdetailToAttach = em.getReference(orderdetailCollectionOrderdetailToAttach.getClass(), orderdetailCollectionOrderdetailToAttach.getId());
                attachedOrderdetailCollection.add(orderdetailCollectionOrderdetailToAttach);
            }
            extraprice.setOrderdetailCollection(attachedOrderdetailCollection);
            em.persist(extraprice);
            if (categoryId != null) {
                categoryId.getExtrapriceCollection().add(extraprice);
                categoryId = em.merge(categoryId);
            }
            if (extraId != null) {
                extraId.getExtrapriceCollection().add(extraprice);
                extraId = em.merge(extraId);
            }
            for (Orderdetail orderdetailCollectionOrderdetail : extraprice.getOrderdetailCollection()) {
                Extraprice oldExtraPriceIdOfOrderdetailCollectionOrderdetail = orderdetailCollectionOrderdetail.getExtraPriceId();
                orderdetailCollectionOrderdetail.setExtraPriceId(extraprice);
                orderdetailCollectionOrderdetail = em.merge(orderdetailCollectionOrderdetail);
                if (oldExtraPriceIdOfOrderdetailCollectionOrderdetail != null) {
                    oldExtraPriceIdOfOrderdetailCollectionOrderdetail.getOrderdetailCollection().remove(orderdetailCollectionOrderdetail);
                    oldExtraPriceIdOfOrderdetailCollectionOrderdetail = em.merge(oldExtraPriceIdOfOrderdetailCollectionOrderdetail);
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Extraprice extraprice) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Extraprice persistentExtraprice = em.find(Extraprice.class, extraprice.getId());
            Category categoryIdOld = persistentExtraprice.getCategoryId();
            Category categoryIdNew = extraprice.getCategoryId();
            Extra extraIdOld = persistentExtraprice.getExtraId();
            Extra extraIdNew = extraprice.getExtraId();
            Collection<Orderdetail> orderdetailCollectionOld = persistentExtraprice.getOrderdetailCollection();
            Collection<Orderdetail> orderdetailCollectionNew = extraprice.getOrderdetailCollection();
            List<String> illegalOrphanMessages = null;
            for (Orderdetail orderdetailCollectionOldOrderdetail : orderdetailCollectionOld) {
                if (!orderdetailCollectionNew.contains(orderdetailCollectionOldOrderdetail)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Orderdetail " + orderdetailCollectionOldOrderdetail + " since its extraPriceId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (categoryIdNew != null) {
                categoryIdNew = em.getReference(categoryIdNew.getClass(), categoryIdNew.getId());
                extraprice.setCategoryId(categoryIdNew);
            }
            if (extraIdNew != null) {
                extraIdNew = em.getReference(extraIdNew.getClass(), extraIdNew.getId());
                extraprice.setExtraId(extraIdNew);
            }
            Collection<Orderdetail> attachedOrderdetailCollectionNew = new ArrayList<Orderdetail>();
            for (Orderdetail orderdetailCollectionNewOrderdetailToAttach : orderdetailCollectionNew) {
                orderdetailCollectionNewOrderdetailToAttach = em.getReference(orderdetailCollectionNewOrderdetailToAttach.getClass(), orderdetailCollectionNewOrderdetailToAttach.getId());
                attachedOrderdetailCollectionNew.add(orderdetailCollectionNewOrderdetailToAttach);
            }
            orderdetailCollectionNew = attachedOrderdetailCollectionNew;
            extraprice.setOrderdetailCollection(orderdetailCollectionNew);
            extraprice = em.merge(extraprice);
            if (categoryIdOld != null && !categoryIdOld.equals(categoryIdNew)) {
                categoryIdOld.getExtrapriceCollection().remove(extraprice);
                categoryIdOld = em.merge(categoryIdOld);
            }
            if (categoryIdNew != null && !categoryIdNew.equals(categoryIdOld)) {
                categoryIdNew.getExtrapriceCollection().add(extraprice);
                categoryIdNew = em.merge(categoryIdNew);
            }
            if (extraIdOld != null && !extraIdOld.equals(extraIdNew)) {
                extraIdOld.getExtrapriceCollection().remove(extraprice);
                extraIdOld = em.merge(extraIdOld);
            }
            if (extraIdNew != null && !extraIdNew.equals(extraIdOld)) {
                extraIdNew.getExtrapriceCollection().add(extraprice);
                extraIdNew = em.merge(extraIdNew);
            }
            for (Orderdetail orderdetailCollectionNewOrderdetail : orderdetailCollectionNew) {
                if (!orderdetailCollectionOld.contains(orderdetailCollectionNewOrderdetail)) {
                    Extraprice oldExtraPriceIdOfOrderdetailCollectionNewOrderdetail = orderdetailCollectionNewOrderdetail.getExtraPriceId();
                    orderdetailCollectionNewOrderdetail.setExtraPriceId(extraprice);
                    orderdetailCollectionNewOrderdetail = em.merge(orderdetailCollectionNewOrderdetail);
                    if (oldExtraPriceIdOfOrderdetailCollectionNewOrderdetail != null && !oldExtraPriceIdOfOrderdetailCollectionNewOrderdetail.equals(extraprice)) {
                        oldExtraPriceIdOfOrderdetailCollectionNewOrderdetail.getOrderdetailCollection().remove(orderdetailCollectionNewOrderdetail);
                        oldExtraPriceIdOfOrderdetailCollectionNewOrderdetail = em.merge(oldExtraPriceIdOfOrderdetailCollectionNewOrderdetail);
                    }
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = extraprice.getId();
                if (findExtraprice(id) == null) {
                    throw new NonexistentEntityException("The extraprice with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Extraprice extraprice;
            try {
                extraprice = em.getReference(Extraprice.class, id);
                extraprice.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The extraprice with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Orderdetail> orderdetailCollectionOrphanCheck = extraprice.getOrderdetailCollection();
            for (Orderdetail orderdetailCollectionOrphanCheckOrderdetail : orderdetailCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Extraprice (" + extraprice + ") cannot be destroyed since the Orderdetail " + orderdetailCollectionOrphanCheckOrderdetail + " in its orderdetailCollection field has a non-nullable extraPriceId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Category categoryId = extraprice.getCategoryId();
            if (categoryId != null) {
                categoryId.getExtrapriceCollection().remove(extraprice);
                categoryId = em.merge(categoryId);
            }
            Extra extraId = extraprice.getExtraId();
            if (extraId != null) {
                extraId.getExtrapriceCollection().remove(extraprice);
                extraId = em.merge(extraId);
            }
            em.remove(extraprice);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Extraprice> findExtrapriceEntities() {
        return findExtrapriceEntities(true, -1, -1);
    }

    public List<Extraprice> findExtrapriceEntities(int maxResults, int firstResult) {
        return findExtrapriceEntities(false, maxResults, firstResult);
    }

    private List<Extraprice> findExtrapriceEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Extraprice.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Extraprice findExtraprice(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Extraprice.class, id);
        } finally {
            em.close();
        }
    }

    public int getExtrapriceCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Extraprice> rt = cq.from(Extraprice.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
